package v9.assist.script

import android.graphics.Color
import sample.ui.ConsoleLayout.Companion.outputText
import sample.ui.Properties
import kotlinx.coroutines.delay
import kotlinx.coroutines.script.Block
import kotlinx.coroutines.script.C
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.random
import kotlinx.coroutines.script.tes.tryLaunchGame
import v9.assist.access.accClick
import v9.assist.access.accClickTraceless
import v9.assist.access.accLastInvokedTs
import v9.assist.access.accPathfindingMapTarget
import v9.assist.access.accPathfinding
import v9.assist.access.btnBlacksmithStart
import v9.assist.access.btnBlacksmithSubmit
import v9.assist.access.btnCenterTop
import v9.assist.access.btnCloseable
import v9.assist.access.btnExitRole
import v9.assist.access.btnImmBackEnter
import v9.assist.access.btnImmFastRestart

import v9.assist.access.btnImmKeyCancel
import v9.assist.access.btnInteract
import v9.assist.access.btnInviteBlackClose
import v9.assist.access.btnMedicine
import v9.assist.access.btnMenuMain
import v9.assist.access.btnMenuPackage
import v9.assist.access.btnMenuPetBlacksmith
import v9.assist.access.btnMenuPetBlacksmithSubmit
import v9.assist.access.btnMenuPetPack
import v9.assist.access.btnMenuSettings
import v9.assist.access.btnMenuSwitchRole
import v9.assist.access.btnNetError
import v9.assist.access.btnNpcAssist
import v9.assist.access.btnPopTxtConfirm
import v9.assist.access.btnRandomBuff
import v9.assist.access.btnReopenGame
import v9.assist.access.btnReopenGameRole
import v9.assist.access.btnReviveByOtherAccept
import v9.assist.access.btnReviveInCity
import v9.assist.access.btnSceneExit
import v9.assist.access.btnSceneInviteGroupAllClose
import v9.assist.access.btnSceneRestart
import v9.assist.access.btnSceneRestartClose
import v9.assist.access.btnSceneStartEnter
import v9.assist.access.btnSceneStartHard
import v9.assist.access.btnSceneStartNormal
import v9.assist.access.btnScreenCenter
import v9.assist.access.btnSelectRole
import v9.assist.access.releaseLastGestures
import v9.assist.analyst.MODE_PATHFINDING_P2
import v9.assist.analyst.MODE_PATHFINDING_P6
import v9.assist.analyst.PAGE_CLOSEABLE
import v9.assist.analyst.PAGE_MAIN
import v9.assist.analyst.PAGE_POP_TEXT
import v9.assist.analyst.PAGE_SCENE_COMPLETED
import v9.assist.analyst.PAGE_SCENE_ENTER
import v9.assist.analyst.PAGE_TALK_BLACKSMITH
import v9.assist.analyst.PAGE_TALK_NPC
import v9.assist.analyst.anaClearSceneSettle
import v9.assist.analyst.anaLastHasEnemyBossNameTs
import v9.assist.analyst.anaLastHasEnemyNameTs
import v9.assist.analyst.anaLastPageNotMainTs
import v9.assist.analyst.anaLastPageTalkBlacksmithTs
import v9.assist.analyst.anaLastSMapYWFeature8Ts
import v9.assist.analyst.anaLastSceneSettleTs
import v9.assist.analyst.anaPathfindingModeFM
import v9.assist.analyst.anaPathfindingModeJT
import v9.assist.analyst.angleByRoad
import v9.assist.analyst.beSceneSettleToBlacksmithByBlueDoor
import v9.assist.analyst.bsHasInteract
import v9.assist.analyst.bsPage
import v9.assist.analyst.bsSceneSettle
import v9.assist.analyst.bsSceneSettleBlacksmith
import v9.assist.analyst.bsSceneSettleBlacksmithMove
import v9.assist.analyst.bsSceneSettleBlueDoorMove
import v9.assist.analyst.lastEliteEnemyBossTs
import v9.assist.analyst.lastEliteEnemyTs
import v9.assist.analyst.lastMapTopNotArrivedTs
import v9.assist.analyst.npcAssistBlock
import v9.assist.analyst.rdRealScene

const val SCRIPT_MAIN = "[*]"

// script 执行的行为
var xScriptEndTs = 0L
var xScriptStartTs = 0L
var xCounterNetRetry = 0
var xLastSceneEnterTs = 0L
var xLastAtkTs = 0L
var xLastAtvTs = 0L
var xLastReviveTs = 0L
var xLastSettleCloseableTs = 0L
var xLastSceneExitTs = 0L
var xLastUseMedicineTs = 0L
var xLastCloseableTs = 0L
var xCounterInteract = 0
var xLastInteractTs = 0L
var xBossDirectOrBuff = false
var xLastBossDirectOrBuffTs = 0L
var xLastUnknownTs = 0L
var xLastSceneCompletedBlacksmithTs = 0L
var xLastSceneCompletedBlacksmithCancelTs = 0L
var xLastPetBlacksmithTs = 0L
var sLastAccInteractTs = 0L
var sLastFeature28Ts = 0L // 裂隙
var sLastFeature29Ts = 0L // 背包分解
var sLastFeature30Ts = 0L // 回头战斗
var sLastFeature31Ts = 0L // 攻击间隔
var sLastFeature32Ts = 0L //
var sLastFeature33Ts = 0L //
var sLastFeature34Ts = 0L //
var sLastFeature35Ts = 0L // npc援助问题
var sLastFeature36Ts = 0L // 互动间隔
var sSceneCompletedTime = 0
var sLastSceneCompletedTs = 0L
var releaseGActionByInteract = 0
var xMYBLOpenState = false
var xSceneHideState = false
var xSceneInvokedExit = false
var xBossBesideTs = 0L // boss就在旁边
var xAtkByBossBesideTs = 0L // 因为boss近身发动的攻击
var xAtvLeftTime = 0L
var xLastEndLogoutTs = 0L

// 副本传送门最大移动次数. 融魂没图标.走5次差不多了
private val sceneSettleBlueDoorMoveMaxTime: Int get() {
//    if (Properties.scene == C.S_RH) return 5
//    return 10
    if (Properties.scene == C.S_RH) return 12
    return 24
}

suspend fun scriptHandlingUnknown() {
    val time = accLastInvokedTs / 1000
    printLog { "x非主页无命中[${time}s]" }  // 有可能是空血
    val interval = 800L // 刷的太快s

    if (time == 2L || time == 3L) {
        releaseLastGestures() // 避免摇杆卡死
    }
    if ((time == 5L || time == 6L || time == 7L)) { // 特别的逻辑
        xSceneInvokedExit = true // 新增一种机制. 回音有效. 有的手机反应快速
    }
    if ((time == 4L || time == 5L)
        && System.currentTimeMillis() - xLastAtkTs < 8000) {
        accClickTraceless(btnMedicine, afterDelay = interval)
    }
    if (Properties.scene == C.S_IMM) {
        if (time == 9L || time == 10L) {
            accClickTraceless(btnImmKeyCancel, afterDelay = interval)
        }
    }
    if (Properties.scene == C.S_FS) {
        if (time == 5L || time == 12L || time == 30L) {
            // 尝试进入钓鱼A
            accClickTraceless(btnInteract, afterDelay = 300)
            accClickTraceless(btnInteract, afterDelay = 300)
        }
    }

    if (time == 10L || time == 30L || time == 32L) {  // 尝试点击下副本进入 // 第二次可能是进入游戏
        if (Properties.forceSceneMode == 2) accClickTraceless(btnSceneStartHard, afterDelay = 800)
        else accClickTraceless(btnSceneStartNormal, afterDelay = 800)

        accClickTraceless(btnSceneStartEnter, afterDelay = interval)
    }
    if (time == 16L || time == 24L || time == 36L) {
        accClickTraceless(btnReviveInCity, afterDelay = interval)
    }
    if (time == 18L) {  // 组队卡输入法. 点2下没事.读秒2秒间隔也不卡逻辑目前
        accClickTraceless(btnCenterTop, afterDelay = interval)
        accClickTraceless(btnCenterTop, afterDelay = interval)
    }
    if (time == 24L || time == 48L) {  // 尝试消除暗影邀请被判定成未知页面
        accClickTraceless(btnInviteBlackClose, afterDelay = interval)
    }
    if (time == 26L || time == 88L) {  // 盲关闭邀请组队弹窗页面
        accClickTraceless(btnSceneInviteGroupAllClose, afterDelay = interval)
    }
    if (time == 28L || time == 60L || time == 96L) {
        accClickTraceless(btnMedicine, afterDelay = interval)
    }
    if (time == 61L || time == 62L) {
        accClickTraceless(btnCenterTop, afterDelay = interval)
    }
    if (time == 30L) {
        if (xCounterNetRetry > 0) {
            xCounterNetRetry--
            accClickTraceless(btnNetError, afterDelay = interval)
        }
    }
    if (time == 36L) {
        accClickTraceless(btnRandomBuff, afterDelay = interval) // 远古副本buff 尝试点击
    }
    if (time == 40L || time == 41L || time == 42L) {
        tryLaunchGame() // 启动恢复游戏
        delay(interval)
    }

    // Properties.scene == C.S_RD beKick 先不要了
}

suspend fun sceneHandlingSettle(blacksmithBy: () ->Block?, exitBy: () ->Block?) {
    printLog { "结算流程 angleByRoad $angleByRoad bsHasInteract $bsHasInteract bsPage $bsPage bsSceneSettle $bsSceneSettle bsSceneSettleBlacksmith $bsSceneSettleBlacksmith" }
    if (bsPage == PAGE_CLOSEABLE) {
        accClick(btnCloseable, afterDelay = 1800) // // 有的人close卡了 铁匠分解完毕.多等一下 避免干扰移动手势
        xLastSettleCloseableTs = System.currentTimeMillis()
        fastMoveToExitByScene()
    } else if (bsPage == PAGE_TALK_BLACKSMITH || bsPage == PAGE_TALK_NPC) {
        // 毁灭副本不分解 因为对话中
        if (!bsSceneSettle) {
            bsSceneSettle = true // 如果直接触发了这个页面. 结算状态直接上车
            anaLastSceneSettleTs = System.currentTimeMillis()
            bsSceneSettleBlacksmith = true
        }
        if (bsSceneSettleBlacksmith) {
            printLog { "分解执行" }
            delay(1000) // 这里按快了不行. 面板打开的很慢 老是被后面的submit取消了
            // 白蓝金 玩家自己勾选
            bsSceneSettleBlacksmith = false // 让分析蓝门那边先变化
            beSceneSettleToBlacksmithByBlueDoor = 0
            accClick(btnBlacksmithStart, afterDelay = 1500)
            accClick(btnBlacksmithSubmit, afterDelay = 1500)
            xLastSceneCompletedBlacksmithTs = System.currentTimeMillis()
        } else {
            accClick(btnScreenCenter, afterDelay = 2000)
            xLastSceneCompletedBlacksmithCancelTs = System.currentTimeMillis() // 阻断交互
        }
    } else if (bsPage == PAGE_SCENE_COMPLETED) {
        if (beSceneSettleToBlacksmithByBlueDoor != 0) {
            beSceneSettleToBlacksmithByBlueDoor = 0
            delay(200) // 界面出来太快.轻微缓一下
            accClick(btnSceneRestartClose, afterDelay = 1500)
        } else {
            delay(1000) // 界面出来太快.轻微缓一下.不然可能点击无效

            // 等待机制
            val waitEndTs = System.currentTimeMillis() + Properties.sceneContinueWait * 1000
            while (waitEndTs > System.currentTimeMillis() && bsPage != PAGE_SCENE_ENTER) {
                printLog { "script. 再次调整等待倒计时 ${(waitEndTs - System.currentTimeMillis())}ms" }
                delay(1000)
            }
            if (bsPage == PAGE_SCENE_COMPLETED) {
                accClick(btnSceneRestart, afterDelay = 2000) // 还是这页面再开始. 可能队友开始了. 多等一下. 不然又触发了疑似互动.导致出去了
            }
            beSceneSettleToBlacksmithByBlueDoor = 0 // 极低的几率是空的申请列表页面误判.这里还原后可以触发下次关闭这个页面
        }
    } else if (bsPage == PAGE_MAIN) {
        if (bsSceneSettleBlacksmithMove > 10) {
            bsSceneSettleBlacksmith = false
        }
        if (bsSceneSettleBlueDoorMove > sceneSettleBlueDoorMoveMaxTime) {
            bsSceneSettle = false
        }
        var exit = false
        val target = if (sceneTryPetBlacksmithSubmit()) { // 增加宠物分解
            bsSceneSettleBlacksmith = false
            bsSceneSettleBlueDoorMove++
            exit = true
            exitBy()
        } else if (bsSceneSettleBlacksmith) {
            bsSceneSettleBlacksmithMove++
            blacksmithBy()
        } else {
            bsSceneSettleBlueDoorMove++
            exit = true
            exitBy()
        }
        printLog { "script. 结算前往目标 [$target] 准备执行 [蓝门M:$bsSceneSettleBlueDoorMove][铁匠M:$bsSceneSettleBlacksmithMove]" }
        if (target != null) {
            accPathfindingMapTarget(target) { index, i ->
                // 跳的快 printLog { "script. 结算前往目标${target.name} $index [蓝门M:$bsSceneSettleBlueDoorMove][铁匠M:$bsSceneSettleBlacksmithMove]" }
                accPathfinding(i)
            }
            releaseLastGestures()
            if (System.currentTimeMillis() - xLastInteractTs > 800
                && System.currentTimeMillis() - xLastSettleCloseableTs > 1200
                && System.currentTimeMillis() - anaLastPageTalkBlacksmithTs > 1200
                && System.currentTimeMillis() - xLastSceneCompletedBlacksmithCancelTs > 2000

                && System.currentTimeMillis() - xLastUnknownTs > 2000) {
                printLog { "script. 结算前往目标${target.name} [互动]" }
                accClick(btnInteract, afterDelay = 800)
                xLastInteractTs = System.currentTimeMillis()
                specialRDAgain(exit)
            }
            specialSettleMove()
            delay(600)
        } else {
            accPathfinding(angleByRoad ?: random.nextInt(360))
        }
    } else {
        val time = accLastInvokedTs / 1000
        printLog { "结算流程画面过渡中[${time}s]" }
        if (time >= 5) anaClearSceneSettle()
        delay(900L)
    }
}

suspend fun sceneExit() {
    // 退出重开
    accClick(btnSceneExit, afterDelay = 1000)
    // 可能是一个x秒进度条？
    accClick(btnReviveByOtherAccept, afterDelay = 13000) // 头顶进度条.久点好.卡进未知界面让xSceneInvokedExit逻辑正常
    xLastSceneExitTs = System.currentTimeMillis()
    xSceneInvokedExit = true
}
suspend fun sceneImmExit() {
    // 退出重开
    if (System.currentTimeMillis() - lastEliteEnemyBossTs in 6000 .. 20000) {
        accClick(btnSceneExit, afterDelay = 1000)
        if (Properties.immRestart != 0) accClick(btnImmFastRestart, afterDelay = 2000)
        else   accClick(btnImmBackEnter, afterDelay = 2000)
    } else {
        accClick(btnSceneExit, afterDelay = 1000)
        accClick(btnReviveByOtherAccept, afterDelay = 8000) // 头顶进度条
    }
    xLastSceneExitTs = System.currentTimeMillis()
}
suspend fun sceneImmTzExit() {
    // 退出重开
    accClick(btnMenuMain, afterDelay = 2000)
    accClick(btnMenuSettings, afterDelay = 2000)
    accClick(btnMenuSwitchRole, afterDelay = 16000)
    accClick(btnSelectRole, afterDelay = 10000)
    xLastSceneExitTs = System.currentTimeMillis()

}

suspend fun tryEnterByRelaunch() {
    if (System.currentTimeMillis() - xLastEndLogoutTs < 60000) {
        printLog { "script. 已定时结束. 忽略重启游戏" }
        return
    }

    repeat(4) {
        if (System.currentTimeMillis() - anaLastPageNotMainTs < 2000) {
            outputText("尝试点击屏幕进入游戏($it)", Color.YELLOW)
            accClick(btnReopenGame, afterDelay = 1000)
        }
    }
    outputText("尝试点击屏幕确定角色", Color.YELLOW)
    delay(300L) // 避免循环过快
    var loadWaitCount = 10
    while (loadWaitCount-- > 0) {
        if (System.currentTimeMillis() - anaLastPageNotMainTs < 2000) {
            if (loadWaitCount > 5) {
                accClickTraceless(btnReopenGameRole)
            }
            outputText("尝试等待载入角色(${loadWaitCount})", Color.YELLOW)
            delay(1000)
        }
    }
}

private var usingLastAngleByRoad: Int? = null

suspend fun sceneAngleByRoad(): Int? {
    if (Properties.scene == C.S_JL || rdRealScene == C.S_JL) {
        if (System.currentTimeMillis() - xLastAtkTs < 6000
            && System.currentTimeMillis() - lastEliteEnemyBossTs in 1000 .. 6000) {
            releaseLastGestures()
            return null // 通关了 原地等一等. 6秒差不多
        }
    } else if (Properties.scene == C.S_HY || rdRealScene == C.S_HY) {
        if (System.currentTimeMillis() - xLastAtkTs < 7000
            && System.currentTimeMillis() - lastMapTopNotArrivedTs > 4000 // 最后的boss并非冰墙boss
            && System.currentTimeMillis() - lastEliteEnemyBossTs in 1000 .. 7000) {
            releaseLastGestures()
            return null // 通关了 原地等一等. 7秒差不多
        }
    } else if (Properties.scene == C.S_SM || rdRealScene == C.S_SM) {
        if (System.currentTimeMillis() - xLastAtkTs < 12000
            && System.currentTimeMillis() - lastEliteEnemyBossTs in 1000 .. 12000) {
            releaseLastGestures()
            return null // 通关了 原地等一等
        }
    } else if (Properties.scene == C.S_YW || rdRealScene == C.S_YW) {
        if (System.currentTimeMillis() - xLastAtkTs < 9000
            && System.currentTimeMillis() - lastEliteEnemyBossTs in 1000 .. 9000) {
            releaseLastGestures()
            return null // 通关了 原地等一等
        }
    } else if ((Properties.scene == C.S_FM || rdRealScene == C.S_FM) && anaPathfindingModeFM == MODE_PATHFINDING_P6) {
        if (System.currentTimeMillis() - xLastAtkTs < 8000
            && System.currentTimeMillis() - lastEliteEnemyBossTs in 1000 .. 8000) {
            releaseLastGestures()
            return null // 通关了 原地等一等 【1层不管. 2层通关等待】
        }
    } else if ((Properties.scene == C.S_JT || rdRealScene == C.S_JT) && anaPathfindingModeJT == MODE_PATHFINDING_P2) {
        if (System.currentTimeMillis() - xLastAtkTs < 10000
            && System.currentTimeMillis() - lastEliteEnemyBossTs in 1000 .. 8000) {
            releaseLastGestures()
            return null // 通关了 原地等一等
        }
    } else if (Properties.scene == C.S_IMM || Properties.scene == C.S_TZ) {
        if (System.currentTimeMillis() - xLastAtkTs < 10000
            && System.currentTimeMillis() - xLastUnknownTs > 12000
            && (System.currentTimeMillis() - lastEliteEnemyBossTs in 1000 .. 10000
                    || System.currentTimeMillis() - anaLastHasEnemyBossNameTs in 1000 .. 10000)) {
            releaseLastGestures()
            return null // 通关了 原地等一等
        }
    }
    if (System.currentTimeMillis() - xLastUnknownTs < 1000) {
        usingLastAngleByRoad = null
        releaseLastGestures()
        return null
    }
    val a = angleByRoad
    if (a != null) {
        usingLastAngleByRoad = a
        return a
    }
    val cacheA = usingLastAngleByRoad
    usingLastAngleByRoad = null
    if (cacheA != null) return cacheA // cache1次上次的角度

    if (Properties.scene == C.S_LX) return null // 裂隙不要随机
    if (Properties.scene == C.S_DYLX) return null
    if (Properties.scene == C.S_DYZZ) return null // 站桩打野不要乱动
    return random.nextInt(360)
}

private suspend fun sceneTryPetBlacksmithSubmit(): Boolean {
//    if (Properties.scenePetClear) {
//        if (Properties.scene == C.S_JL || Properties.scene == C.S_SM || Properties.scene == C.S_HY
//            || Properties.scene == C.S_FB
//            || Properties.scene == C.S_HM || Properties.scene == C.S_JT || Properties.scene == C.S_FW
//        ) { // 不再在终点分解
//            beSceneSettleToBlacksmithByBlueDoor = 0
//            return true
//        }
//    }
    // 宠物分解终点也核算一次
    return tryPetBlacksmithSubmit()
}

suspend fun tryPetBlacksmithSubmit(): Boolean {
    if (Properties.scenePetClear) printLog { "script 执行使魔分解" }
    if (Properties.scenePetClear
        && System.currentTimeMillis() - xScriptStartTs > 20000
        && System.currentTimeMillis() - xLastSceneCompletedBlacksmithTs > 30000
        && System.currentTimeMillis() - xLastPetBlacksmithTs > 30000
        && System.currentTimeMillis() - xLastReviveTs > 20000
        && System.currentTimeMillis() - lastEliteEnemyTs > 3000
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 3000
        && System.currentTimeMillis() - anaLastHasEnemyNameTs > 1000) {
        printLog { "script 执行使魔分解 invoke" }
        accClick(btnMenuPackage, afterDelay = 3500) // 太卡了.屏幕.避免没打开
        accClick(btnMenuPetPack, afterDelay = 1500)
        accClick(btnMenuPetBlacksmith, afterDelay = 3500) // 避免卡了错过了分解 1500 -> 3500
        accClick(btnMenuPetBlacksmithSubmit, afterDelay = 2000) // 避免地图检测目标慢.导致 bsSceneSettleBlueDoorMove 增长快
        beSceneSettleToBlacksmithByBlueDoor = 0
        xLastPetBlacksmithTs = System.currentTimeMillis()
        return true
    }
    if (System.currentTimeMillis() - xLastPetBlacksmithTs < 30000) {
        printLog { "script 执行使魔分解 刚已执行" }
        return true
    }
    return false
}

private suspend fun fastMoveToExitByScene() {
    if (Properties.scene == C.S_JL || rdRealScene == C.S_JL) {
        listOf(340, 350, 0, 0, 0).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 4) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_HY || rdRealScene == C.S_HY) {
        listOf(106, 106, 106, 106).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 3) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_FB || rdRealScene == C.S_FB) {
        listOf(35, 35, 40, 0, 345).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 4) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_HM || rdRealScene == C.S_HM) {
        listOf(115, 115, 115, 110).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 3) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_FW || rdRealScene == C.S_FW) {
        listOf(105, 105, 105, 105).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 3) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_SM || rdRealScene == C.S_SM) {
        listOf(135, 135, 135, 135).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 3) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_YW || rdRealScene == C.S_YW) {
        listOf(270, 270, 270, 270, 260).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 4) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_FM || rdRealScene == C.S_FM) {
        listOf(330, 330, 330, 330, 330, 330, 330).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" }
            accPathfinding(i, if (index == 6) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_RH || rdRealScene == C.S_RH) {
        listOf(135, 135, 135, 135, 135, 135, 135, 135).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" } // 在顶端
            accPathfinding(i, if (index == 7) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    } else if (Properties.scene == C.S_MY || rdRealScene == C.S_MY) {
        listOf(5, 5, 5, 5).forEachIndexed { index, i ->
            printLog { "结算分解快速调整至再次调整 $index" } // 在顶端
            accPathfinding(i, if (index == 3) btnInteract else null)
        }
        releaseLastGestures()
        specialRDAgain()
        delay(800)
    }
}

private suspend fun specialRDAgain(exit: Boolean = true) {
    if (Properties.scene == C.S_RD && exit) {
        // 随机本.这里尝试点击POP.TEXT.CONFIRM继续下一把
        var counter = 6
        while (counter -- > 0) {
            if (bsPage == PAGE_POP_TEXT) {
                printLog { "随机本.前往传送门后.直接尝试下一把" }
                accClick(btnPopTxtConfirm, afterDelay = 3000)
            }
            delay(300L)
        }
    }
}

private suspend fun specialSettleMove() {
    if (Properties.scene == C.S_FW || rdRealScene == C.S_FW) {
        if (bsSceneSettleBlueDoorMove == 7) {
            printLog { "script specialSettleMove 也许卡在了传送门下面.尝试调整" }
            listOf(280, 280, 280, 280).forEachIndexed { index, i ->
                printLog { "script 疯王结算卡路 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
            delay(1000L)
        }
    } else if (Properties.scene == C.S_YW || rdRealScene == C.S_YW) {
        if (System.currentTimeMillis() - anaLastSMapYWFeature8Ts < 2000
            && bsSceneSettleBlueDoorMove == 7) {
            listOf(130, 140, 170, 180, 230).forEachIndexed { index, i ->
                printLog { "script 遗忘结算卡路 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
            delay(1000L)
        }
    }
}

suspend fun accSceneEnter() {
    releaseLastGestures()
    delay(1500L) // 避免点击不灵?
    if (Properties.forceSceneMode == 2) {
        accClick(btnSceneStartHard, afterDelay = 800)
        accClick(btnSceneStartHard, afterDelay = 800) // 不灵点2下？
    }
    else if (Properties.forceSceneMode == 1) {
        accClick(btnSceneStartNormal, afterDelay = 800)
        accClick(btnSceneStartNormal, afterDelay = 800) // 不灵点2下？
    }

    if (System.currentTimeMillis() - sLastFeature35Ts > 16000) { // 避免卡死点击
        npcAssistBlock?.also {
            accClick(btnNpcAssist, afterDelay = 800)
            accClick(btnCenterTop, afterDelay = 1000)
        }
    }
    accClick(btnSceneStartEnter, afterDelay = 800) // 避免不灵
    accClick(btnSceneStartEnter, afterDelay = 1600) // 避免不灵


    rdRealScene = null
    if (System.currentTimeMillis() - sLastSceneCompletedTs > 30000) sSceneCompletedTime += 1
    sLastSceneCompletedTs = System.currentTimeMillis()
    anaClearSceneSettle()
    xLastSceneEnterTs = System.currentTimeMillis()
    xLastAtvTs = System.currentTimeMillis()
    xBossDirectOrBuff = false
    sLastFeature35Ts = System.currentTimeMillis()

}


suspend fun errorEndLogout() {
    if (bsPage == PAGE_MAIN) {
        accClick(btnMenuMain, afterDelay = 2000)
        accClick(btnMenuSettings, afterDelay = 2000)
        accClick(btnMenuSwitchRole, afterDelay = 24000)
        accClick(btnExitRole, afterDelay = 5000) // 完全退出.防止站街封号
        xLastEndLogoutTs = System.currentTimeMillis()
    }
}